package example.springboot.controller;

import example.springboot.common.Result;
import example.springboot.dto.WordbookDTO;
import example.springboot.entity.Word;
import example.springboot.entity.Wordbook;
import example.springboot.service.WordbookService;
import example.springboot.util.SystemLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
@RestController
@RequestMapping("/api/admin/wordbooks")
@CrossOrigin
public class WordbookController {

    @Autowired
    private WordbookService wordbookService;

    /**
     * 获取所有词书列表
     */
    @GetMapping
    public Result<List<WordbookDTO>> getAllWordbooks() {
        try {
            List<WordbookDTO> result = wordbookService.getAllWordbooks();
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取词书列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取词书详情
     */
    @GetMapping("/{id}")
    public Result<Wordbook> getWordbookById(@PathVariable Integer id) {
        try {
            Wordbook wordbook = wordbookService.findById(id);
            if (wordbook == null) {
                return Result.error("词书不存在");
            }
            return Result.success(wordbook);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取词书详情失败：" + e.getMessage());
        }
    }

    /**
     * 创建词书
     */
    @PostMapping
    public Result<Wordbook> createWordbook(@RequestBody Wordbook wordbook) {
        try {
            // 验证必填字段
            if (wordbook.getName() == null || wordbook.getName().trim().isEmpty()) {
                return Result.error("词书名称不能为空");
            }

            Wordbook createdWordbook = wordbookService.createWordbook(wordbook);
            // 记录操作日志
            SystemLogUtil.logOperation("创建词书", "创建了词书: " + wordbook.getName());
            return Result.success("创建成功", createdWordbook);
        } catch (RuntimeException e) {
            // 记录错误日志
            SystemLogUtil.logError("创建词书失败", "创建词书失败: " + e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            // 记录错误日志
            SystemLogUtil.logError("创建词书失败", "创建词书失败: " + e.getMessage());
            return Result.error("创建词书失败：" + e.getMessage());
        }
    }

    /**
     * 更新词书信息
     */
    @PutMapping("/{id}")
    public Result<String> updateWordbook(@PathVariable Integer id, @RequestBody Wordbook wordbook) {
        try {
            wordbook.setId(id);
            boolean success = wordbookService.updateWordbook(wordbook);
            if (success) {
                // 记录操作日志
                SystemLogUtil.logOperation("编辑词书", "编辑了词书ID: " + id);
                return Result.success("更新成功");
            } else {
                // 记录错误日志
                SystemLogUtil.logError("编辑词书失败", "编辑词书ID: " + id + " 失败");
                return Result.error("更新失败");
            }
        } catch (RuntimeException e) {
            // 记录错误日志
            SystemLogUtil.logError("编辑词书失败", "编辑词书ID: " + id + " 失败: " + e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            // 记录错误日志
            SystemLogUtil.logError("编辑词书失败", "编辑词书ID: " + id + " 失败: " + e.getMessage());
            return Result.error("更新词书失败：" + e.getMessage());
        }
    }

    /**
     * 删除词书
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteWordbook(@PathVariable Integer id) {
        try {
            boolean success = wordbookService.deleteWordbook(id);
            if (success) {
                // 记录操作日志
                SystemLogUtil.logOperation("删除词书", "删除了词书ID: " + id);
                return Result.success("删除成功");
            } else {
                // 记录错误日志
                SystemLogUtil.logError("删除词书失败", "删除词书ID: " + id + " 失败: 词书不存在");
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 记录错误日志
            SystemLogUtil.logError("删除词书失败", "删除词书ID: " + id + " 失败: " + e.getMessage());
            return Result.error("删除词书失败：" + e.getMessage());
        }
    }

    /**
     * 根据词书ID获取单词列表
     */
    @GetMapping("/{id}/words")
    public Result<List<Word>> getWordbookWords(@PathVariable Integer id) {
        try {
            List<Word> words = wordbookService.getWordsByWordbookId(id);
            return Result.success(words);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取单词列表失败：" + e.getMessage());
        }
    }

    /**
     * 从词书中移除单词
     */
    @DeleteMapping("/{wordbookId}/words/{wordId}")
    public Result<String> removeWordFromWordbook(@PathVariable Integer wordbookId, @PathVariable Integer wordId) {
        try {
            boolean success = wordbookService.removeWordFromWordbook(wordbookId, wordId);
            if (success) {
                return Result.success("移除成功");
            } else {
                return Result.error("移除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("移除单词失败：" + e.getMessage());
        }
    }
}

